package com.hz.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hz.web.dao.MenuDao;
import com.hz.web.entity.MenuEntity;
import com.hz.web.entity.RoleMenuEntity;
import com.hz.web.service.ImenuService;

@SuppressWarnings("all")
@Service
public class MenuImpl implements ImenuService {

	@Resource
	private MenuDao menuDao;
	
	/**
	 * 查询菜单集合
	 */
	@Override
	public List<MenuEntity> queryNodes(Map<String, Object> map ) {
		
		List<MenuEntity> nodes = menuDao.queryNodes(map);
	
		return nodes;
	}

	/**
	 * 查询菜单树 
	 * @param nodes
	 * @return
	 */
	/*public List<Menu> buildTree(List<Menu> nodes){  
        
    	List<Menu> fuls = new ArrayList<Menu>();
        for (Menu node : nodes) {   
            if (node.getParentId() == 0) {  
            	List<Menu> child= build(node,nodes); 
            	
            	node.setChildren(child);;
            	fuls.add(node);	
            }   
        }  
        return fuls;
    } */ 
      
	/**
	 * 查询菜单树 
	 * @param nodes
	 * @return
	 */
    private List<MenuEntity> build(MenuEntity node,Map<String, Object> controllerMap){ 
    	
        List<MenuEntity> children = getChildren(node,controllerMap);  
        if (!children.isEmpty()) {  
        	 if(node.getChildren()==null){
             	List<MenuEntity> ls = new ArrayList<MenuEntity>();
             	node.setChildren(ls);;
             } 
             node.setChildren(children);    
            for (MenuEntity child : children) {  
                build(child,controllerMap);  
            }  
        
        }  
        
        return children;
    }  
      
    /**
     * 传过来的参数的id，如果和集合里面的 父id   查询子菜单
     * @param node
     * @return
     */
    private List<MenuEntity> getChildren(MenuEntity node,Map<String, Object> controllerMap){  
        List<MenuEntity> children = new ArrayList<MenuEntity>();  
        int id = node.getId();  
     
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id",id);
        //判断传过来的map集合里面有没有   （路径类型）,如果有mapping文件判断是哪个路径的
        String urlType= (String) controllerMap.get("urlType");
        if(urlType!=null && !"".equals(urlType)){ 	 
          	 map.put("urlType", urlType);
           }
        //判断传过来的map集合里面有没有   （已分配权限的集合）,如果有mapping文件进行控制判断
        List<RoleMenuEntity> list = (List<RoleMenuEntity>) controllerMap.get("list");
        if(list!=null){
        	
        	map.put("list", list);
        }
      //判断传过来的map集合里面有没有   （未分配权限的集合）,如果有mapping文件进行控制判断
        List<RoleMenuEntity> noAddls = (List<RoleMenuEntity>) controllerMap.get("noAddls");
        if(noAddls!=null){ 	
        	map.put("noAddls", noAddls);
        }   
       
        children= queryNodesTree(map);
        /* List<Menu> nodes= queryNodesTree(map);
        for (Menu child : nodes) {  
            if (id==child.getParentId()) {  
                children.add(child);  
            }  
        }*/  
        return children;  
    }

    /**
     * 查询子菜单
     */
	@Override
	public List<MenuEntity> queryNodesTreeOnly(Map<String, Object> controllerMap) {
		
		List<MenuEntity> nodes = menuDao.queryNodesTree(controllerMap);
		return nodes;
	} 
	
    /**
     * 查询子菜单
     */
	@Override
	public List<MenuEntity> queryNodesTree(Map<String, Object> controllerMap) {
		
		List<MenuEntity> nodes = menuDao.queryNodesTree(controllerMap);
		return testTree(nodes,controllerMap);
	}  
	
	public List<MenuEntity> testTree(List<MenuEntity> nodes,Map<String, Object> controllerMap){  
        
    	List<MenuEntity> fuls = new ArrayList<MenuEntity>();
        for (MenuEntity node : nodes) {   
      
            	List<MenuEntity> child= build(node,controllerMap); 
            	
            	node.setChildren(child);;
            	fuls.add(node);	
           
        }  
        return fuls;
    }

	/**
	 * 插入菜单信息
	 */
	@Override
	public int saveMenu(MenuEntity menu) {
		int rows=-1;
		try {
			rows = menuDao.saveMenu(menu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return rows;
	}

	/**
	 * 修改菜单
	 * @param menu
	 * @return
	 */
	@Override
	public int updateMenu(MenuEntity menu) {
		int rows=-1;
		try {
			 rows = menuDao.updateMenu(menu);		 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rows;
	}

	 /**
     * 查询子菜单不是树的形式
     */
	@Override
	public List<MenuEntity> queryNodesNoTree(Map<String, Object> controllerMap) {
		
		List<MenuEntity> nodes = menuDao.queryNodesTree(controllerMap);
		return nodes;
	} 
}
